package com.zhe.zheblogmaster.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhe.zheblogmaster.config.BlogConstants;
import com.zhe.zheblogmaster.entity.Link;
import com.zhe.zheblogmaster.mapper.LinkMapper;
import com.zhe.zheblogmaster.service.LinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.chrono.IsoChronology;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author WHAOU
 */
@Service
public class LinkServiceImpl implements LinkService {
    @Autowired
    private LinkMapper linkMapper;

    @Override
    public Integer getTotalLinks() {
        return linkMapper.selectCount();
    }

    @Override
    public Map<Byte, List<Link>> getLinks() {
        List<Link> links = linkMapper.selectLinks();
        if (!CollectionUtils.isEmpty(links)) {
            return links.stream().collect(Collectors.groupingBy(Link::getLinkType));
        }
        return null;
    }

    @Override
    public PageInfo<Link> getLinkPageInfo(Integer pageNum) {
        PageHelper.startPage(pageNum, BlogConstants.DEFAULT_PAGE_SIZE);
        List<Link> links = linkMapper.selectLinks();
        return new PageInfo<>(links);
    }

    @Override
    public int addLink(Link link) {
        return linkMapper.insertLinkSelective(link);
    }

    @Override
    public Link selectByPrimaryKey(Integer id) {
        return linkMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Link link) {
        return linkMapper.updateByPrimaryKeySelective(link);
    }

    @Override
    public int deleteBatch(Integer[] ids) {
        return linkMapper.deleteByPrimaryKeyBatch(ids);
    }
}
